# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def hasCycle(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        m = {}
        while head:
            if head in m:
                return True
            else:
                m[head] = 1
            head = head.next
        return False

    def hasCycle2(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        fp = head
        sp = head
        while fp and fp.next and sp:
            sp = sp.next
            fp = fp.next.next 
            if sp == fp or (fp and sp == fp.next):
                return True
        return False
